package com.hnu.service.search;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;
import org.wltea.analyzer.lucene.IKQueryParser;
import org.wltea.analyzer.lucene.IKSimilarity;

import com.hnu.model.Bookmark;

public class LuceneUtil {
	public static void indexBookmark(Bookmark bookmark, String indexPath) {
		Directory dir;
		try {
			dir = FSDirectory.open(new File(indexPath));
			Analyzer luceneAnalyzer = new IKAnalyzer(); // ����һ����׼������
			IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_31,
					luceneAnalyzer);
			iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
			IndexWriter indexWriter = new IndexWriter(dir, iwc);
			Document doc = new Document();
			doc.add(new Field("bookmarkId", String.valueOf(bookmark
					.getBookmarkId()), Field.Store.YES, Field.Index.NO));
			doc.add(new Field("bookmarkTitle", bookmark.getBookmarkTitle(),
					Field.Store.YES, Field.Index.ANALYZED));
			doc.add(new Field("bookmarkUrl", bookmark.getBookmarkUrl(),
					Field.Store.YES, Field.Index.NO));
			doc.add(new Field("bookmarkDesc", bookmark.getBookmarkDesc(),
					Field.Store.YES, Field.Index.ANALYZED));
			indexWriter.addDocument(doc);
			indexWriter.optimize();
			indexWriter.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		}

	}

	public static List<Bookmark> searchBookmark(String key, String indexPath,
			int howmany) {
		List<Bookmark> bookmarkList = new ArrayList<Bookmark>();
		if ((key != null) && (!key.isEmpty())) {
			Directory dir;

			try {
				dir = FSDirectory.open(new File(indexPath));
				/*Analyzer luceneAnalyzer = new StandardAnalyzer(
						Version.LUCENE_31); // ����һ����׼������*/
				IndexSearcher indexSearch = new IndexSearcher(dir);
				indexSearch.setSimilarity(new IKSimilarity());
				String[] fields = { "bookmarkTitle", "bookmarkDesc" };
				/*MultiFieldQueryParser mp = new MultiFieldQueryParser(
						Version.LUCENE_31, fields, luceneAnalyzer);
				Query query = mp.parse(key);*/
				Occur[] flags = new BooleanClause.Occur[]{Occur.SHOULD,Occur.SHOULD};
				
				Query query = IKQueryParser.parseMultiField(fields, key, flags);
				TopDocs results = indexSearch.search(query, howmany);
				ScoreDoc[] hits = results.scoreDocs;
				int count = results.totalHits;
				Bookmark bookmark;
				Document doc;
				for (int i = 0; i < count; i++) {
					doc = indexSearch.doc(hits[i].doc);
					bookmark = new Bookmark();
					bookmark.setBookmarkId(Long.parseLong(doc.get("bookmarkId")));
					bookmark.setBookmarkDesc(doc.get("bookmarkDesc"));
					bookmark.setBookmarkTitle(doc.get("bookmarkTitle"));
					bookmark.setBookmarkUrl(doc.get("bookmarkUrl"));
					bookmarkList.add(bookmark);

				}
				indexSearch.close();
			} catch (Exception e) {

				//e.printStackTrace();
			}
		}

		return bookmarkList;
	}

}
